package com.ywjh.inter.service;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.ywjh.inter.utils.HTTPHelper;

public class SmsService {

	private String smsIP = "47.100.76.43:18002";
	private String uid = "1520";
	private String pw = "642057";

	//营销短信
//	private String uid = "1636";
//	private String pw = "642057";
	private static SmsService instance = null;

	public static SmsService getInstance() {
		if (instance == null) {
			instance = new SmsService();
		}
		return instance;
	}

	public static void main(String[] args) {
		SmsService.getInstance().sendSmsCode("123456","13269039325", "58.216.213.34","登陆");
	}

	public boolean sendSmsCode(String code, String phone, String ip,String what) {
		boolean b = false;
		String head = "【灿生医疗】";
		String content = head + "您"+what+"的验证码为:" + code + ",验证码十分钟内有效，请勿泄漏于他人!" + "系统已防止第三方应用恶意读取和使用验证 切勿泄露他人。";
		try {
			content = URLEncoder.encode(content,"UTF-8");
		}catch (Exception e){
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String url = "http://" + smsIP + "/send.do?uid=" + uid + "&pw=" + pw + "&mb=" + phone + "&ms=" + content
				+ "&ex=01&tm=" + sdf.format(new Date(System.currentTimeMillis())) + "";
		System.out.println(url+"-"+ip);
		Map<String, String> header = new HashMap<>();
		header.put("x-forwarded-for", ip);
		String result;
		try {
			result = HTTPHelper.getData(url, header);
		} catch (IOException e) {
			result="0";
		}
		String resultmsg = "";
		// 返回值,批次号
		switch (result) {
		case "0":
			resultmsg = "服务器异常";
			break;
		case "-1":
			resultmsg = "uid 或密码为空";
			break;
		case "-2":
			resultmsg = "下发号码为空";
			break;
		case "-3":
			resultmsg = "下发内容为空";
			break;
		case "-4":
			resultmsg = "内容超长";
			break;
		case "-5":
			resultmsg = "下发号码超长";
			break;
		case "-6":
			resultmsg = "uid 或密码不正确";
			break;
		case "-7":
			resultmsg = "余额不足";
			break;
		case "-8":
			resultmsg = "IP 地址鉴权不对";
			break;
		case "-9":
			resultmsg = "协议类型不对";
			break;
		case "-99":
			resultmsg = "系统异常";
			break;
		default:
			resultmsg = "成功:" + result;
			b = true;
			break;
		}
		System.out.println("send sms result=" + resultmsg);
		return b;

	}
}
